// packageA/signUp/signUp.js
import {
  areaList
} from '@vant/area-data';
import {
  postEnroll
} from "../../api/news.js"
import {
  carList
} from "../../api/car.js"
import {
  captchaImage,
  getcodeNew
} from "../../api/index.js"
const regx = /^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaList,
    show: false,
    show1:false,
    show2:false,
    showNew: false,
    cellTxt: '选择城市',
    categoryName: '选择车型',
    automoList: [],
    name: '',
    phone: '',
    checked: false,
    code: '',
    city: '',
    province: '',
    obtain: '获取验证码',
    img_code: '',
    short: '',
    topicId: '',
    TIME_COUNT: 120, // 验证码倒计时60s
    count: '',
    issendcode: true,
    timer: null,
    disabled:false
  },
  preventTouchMove(){

  },
  onChoiceChange(event) {
    this.setData({
      checked: !this.data.checked,
    });
  },
  showPopup() {
    this.setData({
      show: true,
      disabled:true
    });
  },
  async enroll() {
    var {
      name,
      phone,
      city,
      province,
      code,
      categoryName,
      topicId,
      checked
    } = this.data
    if (name == '') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    }
    if (categoryName == '') {
      wx.showToast({
        title: '请选择车型',
        icon: 'none'
      })
      return
    }
    if (phone == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return
    }
    if (!regx.test(phone)) {
      wx.showToast({
        title: '请填写正确的手机号',
        icon: 'none'
      })
      return
    }
    if (city == '') {
      wx.showToast({
        title: '请选择城市',
        icon: 'none'
      })
      return
    }
    if (province == '') {
      wx.showToast({
        title: '请选择省份',
        icon: 'none'
      })
      return
    }
    if (code == '') {
       wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
      return
    }
    if (!checked) {
      wx.showToast({
       title: '请阅读并同意隐私条款',
       icon: 'none'
     })
     return
   }
    let data = {
      name,
      phone,
      city,
      province,
      params: {
        code
      },
      categoryName,
      topicId
    };
    console.log("登录")
    var res = await postEnroll(data)
    if(res.code == 200){
      wx.showToast({
        title: '报名成功！',
        success:function(){
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 2000);
        }
      })
    }
    this.captchaImage()
  },
  onInputChange(e) {
    this.setData({
      name: e.detail
    })
  },
  onphoneChange(e) {
    this.setData({
      phone: e.detail
    })
  },
  shortChange(e) {
    this.setData({
      code: e.detail
    })
  },
  graphicalChange(e) {
    this.setData({
      img_code: e.detail
    })
  },
  onClose() {
    // this.setData({
    //   show: false,
    // });
  },
  autoPopup() {
    this.setData({
      showNew: true,
      disabled:true
    });
  },
  onAutomoCancel() {
    this.setData({
      showNew: false,
      disabled:false
    });
  },
  clickXieyi() {
    this.setData({
      show1: true
    })
  },
  clickYinsi() {
    this.setData({
      show2: true
    })
  },
  useAgree(e) {
    var checked = e.detail.checked
    this.setData({
      checked: checked
    })
  },
  onAutomoConfirm(e) {
    this.setData({
      showNew: false,
      disabled:false,
      categoryName: e.detail.value.name
    });
  },
  wancheng(e) {
    this.setData({
      show: false,
      disabled:false,
      cellTxt: e.detail.values[0].name + e.detail.values[1].name,
      city: e.detail.values[1].name,
      province: e.detail.values[0].name,
    });
  },
  quxiao(e) {
    this.setData({
      show: false,
      disabled:false
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      topicId: options.id
    })
    this.carList()
    this.captchaImage()
  },
  async carList() {
    var res = await carList(1)
    this.setData({
      automoList: res.category
    })
  },
  async captchaImage() {
    var res = await captchaImage()
    this.setData({
      img: res.img = 'url(data:image/gif;base64,' + res.img + ')',
      uuid: res.uuid
    })
  },
  async obtainClick() {
    var {
      phone,
      img_code,
      uuid
    } = this.data
    let data = {
      phone,
      img_code,
      uuid
    }
    var res = await getcodeNew(data)
    if(res.code == 200){
      wx.showToast({
        title: '验证码已发送至您的手机，请注意查收~',
        icon: "none"
      })
      if (!this.data.timer) {
        const phoneNumber = String(phone)
        this.setData({
          count: this.data.TIME_COUNT,
          issendcode: false
        })
        this.data.timer = setInterval(() => {
          if (this.data.count > 0 && this.data.count <= this.data.TIME_COUNT) {
            this.setData({
              count: this.data.count - 1
            })
          } else {
            clearInterval(this.data.timer)
            this.data.timer = null
            this.setData({
              issendcode: true
            })
          }
        }, 1000)
      }
    }else if(res.code == 500){
      this.captchaImage()
      wx.showToast({
        title: res.msg,
        icon: "none"
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})